﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace YouHP.BLL.MedicalGraph
{
    public class GraphBase
    {
        public GraphSetting Setting { get; set; }
        public GraphDataManager Data { get; set; }
        public GraphResult Result { get; set; }
        public GraphStyle Style { get; set; }

        protected GraphRule Rule { get; set; }                



        public int _GridCount;
        protected int _XSpacing = 30;
        public DateTime _EndDateTime;
        public DateTime _StartDateTime;
        public int MonthCount;

        public string _Message;
        protected string mLegendName;

        protected GraphBase()
        {
            _Message = "";

            Result = new GraphResult();
            Style = new GraphStyle();

        }

        public void SetMessage(string msg)
        {
            _Message = msg;
        }
        public void SetLegendName(string name)
        {
            mLegendName = name;
        }

        protected string mLegendPath = "lengend.png";
        protected string mDataPath = "data.png";
        protected string mWebLegendPath = "lengend.png";
        protected string mWebDataPath = "data.png";
        protected string SavePath { get; set; }
        protected string WebPath { get; set; }
        protected int Suffix;   // 文件名后缀
        internal void SetFilePath(string lpath, string dpath)
        {
            mLegendPath = lpath;
            mDataPath = dpath;
        }
        public void SetPath(string path)
        {
            SavePath = path;
            if (Directory.Exists(path) == false) Directory.CreateDirectory(path);
        }
        public void SetWebPath(string path)
        {
            WebPath = path;
        }


        #region draw util
        protected void DrawMessage()
        {
            if (_Message == "") return;
            Rectangle rect1 = new Rectangle(10, _DataRect.Bottom + 10, Rule.LegendWidth - 20, Rule.Height - _DataRect.Bottom - 10);
            StringFormat dformat = new StringFormat();
            dformat.Alignment = StringAlignment.Center;
            dformat.LineAlignment = StringAlignment.Center;
            mGraphicsLegend.FillRectangle(Style.MessageBoxBrush, rect1);
            mGraphicsLegend.DrawString(_Message, Style.MessageFont, Style.MessageBrush, rect1, dformat);
        }
        protected void DrawDataTime(DateTime date, int x1, int y1)
        {
            _GraphicsData.DrawString(date.ToString("yyyy"),
                   Rule.mGridFont, Brushes.Black, x1, y1, Rule.mLabelFotmat);
            y1 += 10;
            _GraphicsData.DrawString(date.ToString("MM"),
               Rule.mGridFont, Brushes.Black, x1, y1, Rule.mLabelFotmat);
        }
        #endregion

        #region Axis
        protected virtual double GetDataX(int index, DateTime date)
        {
            int month = 12 * (date.Year - _StartDateTime.Year);
            month += date.Month - _StartDateTime.Month;
            double x = _DataRect.Left + _XSpacing * month + _XSpacing;
            x += _XSpacing * date.Day / DateTime.DaysInMonth(date.Year, date.Month);
            return x;
        }
        protected virtual double GetDataX(int index, int x)
        {
            return _DataRect.Left + _XSpacing * x;
        }

        protected virtual double GetDataY(int index, DateTime date)
        {
            return GetDataY(index, 0);
        }
        protected virtual double GetDataY(int index, double y)
        {
            return _DataRect.Bottom - _DataRect.Height * Rule.GetVariable(index).GetRelPosition2(y);

        }
        #endregion

        #region Draw

        protected virtual void DrawData()
        {

        }

        protected virtual void DrawDataGrid()
        {
 
        }

        protected virtual void DrawLegend()
        {

        }

        protected virtual void DrawLegendGrid()
        {

        }

        #endregion

        #region virtual function
        protected virtual void SetupAxis()
        {
            int cnt = Rule.GetVariableCount();
            double per = 1.0f;
            double temp;
            int i;
            for (i = 0; i < cnt; i++)
            {
                temp = Rule.GetVariable(i).GetNormalPosition();
                if (per > temp) per = temp;
            }
            for (i = 0; i < cnt; i++)
            {
                Rule.GetVariable(i).SetNormalPosition(per);
            }
        }

        protected virtual void SetupPage()
        {
        }

        #endregion

        Bitmap mBitmapLegend;
        Bitmap _BitmapData;
        protected Graphics mGraphicsLegend;
        protected Graphics _GraphicsData;


        private void CreateGraph()
        {
            SetupPage();
            SetupAxis();
            CreateLegendGraph();
            CreateDataGraph();
            return;
        }




        protected Rectangle _DataRect;

        private void CreateDataGraph()
        {
            _BitmapData = new Bitmap(Rule.DataWidth, Rule.Height);
            _GraphicsData = Graphics.FromImage(_BitmapData);
            _GraphicsData.FillRectangle(Brushes.White, 0, 0, Rule.DataWidth, Rule.Height);

            DrawDataGrid();

            if (Data != null) DrawData();
            _GraphicsData.Dispose();
            _BitmapData.Save(this.mDataPath, System.Drawing.Imaging.ImageFormat.Png);
            _BitmapData.Dispose();

        }
        private void CreateLegendGraph()
        {
            mBitmapLegend = new Bitmap(Rule.LegendWidth, Rule.Height);
            Graphics g = Graphics.FromImage(mBitmapLegend);
            mGraphicsLegend = g;
            g.FillRectangle(Brushes.White, 0, 0, Rule.LegendWidth, Rule.Height);

            DrawLegend();
            DrawLegendGrid();
            DrawMessage();

            mBitmapLegend.Save(this.mLegendPath, ImageFormat.Png);
            mBitmapLegend.Dispose();
        }


       

        

        public void MakeGraph()
        {
            int i = 1;
            foreach (GraphRule rule in Setting.ParamList)
            {
                Suffix = i;
                Rule = rule;
                CreateGraph();
                i++;
            }
        }

    }

}
